<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\housing;

use app\common\dao\BaseDao;
use app\common\model\housing\HousingResourceModel;

class HousingResourceDao extends BaseDao
{

    /**
     * @return HousingResourceModel
     */
    protected function getModel(): string
    {
        return HousingResourceModel::class;
    }

    public function search(array $where)
    {
        return $this->getModel()::getDB()
            ->when(isset($where['property_id']) && $where['property_id'] !== '', function ($query) use ($where) {
                $query->where('property_id', (int)$where['property_id']);
            })
            ->when(isset($where['cate_id']) && $where['cate_id'] !== '', function ($query) use ($where) {
                $query->where('cate_id', (int)$where['cate_id']);
            })
            ->when(isset($where['label_id']) && $where['label_id'] !== '', function ($query) use ($where) {
                $query->whereIn('id', function ($query) use ($where) {
                    $query->name('housing_label')->where('label_id', $where['label_id'])->field('room_id');
                });
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('remark|housing_code|housing_title', '%' . trim($where['keywords']) . '%');
            });
    }

}
